package com.wohuijituan.whcsmerchant.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.bean.IsSuccessBean;
import com.wohuijituan.whcsmerchant.callback.IsSuccessCallBack;
import com.wohuijituan.whcsmerchant.url.ParamsKey;
import com.wohuijituan.whcsmerchant.url.Urls;

import okhttp3.Call;

/**
 * Created by zsy_18 on 2016/12/27.
 */

public class ReleaseNoticeActivity extends MyBaseActivity implements View.OnClickListener{
    private TextView tvReleaseNotice;
    private EditText etNoticeContent;
    private Button btnReleaseNotice;
    private ImageButton titleGoBack;
    private TextView titleName;
    private TextView titleTvAction;
    private ImageButton titleIbAction;
    private void assignViews() {
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        titleTvAction = (TextView) findViewById(R.id.title_tv_action);
        titleIbAction = (ImageButton) findViewById(R.id.title_ib_action);
        tvReleaseNotice = (TextView) findViewById(R.id.tv_release_notice);
        etNoticeContent = (EditText) findViewById(R.id.et_notice_content);
        btnReleaseNotice = (Button) findViewById(R.id.btn_release_notice);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_notice);
        assignViews();
        initData();
        initView();
        initListener();
    }
    private void initData() {

    }

    private void initView() {
        titleGoBack.setVisibility(View.VISIBLE);
        titleName.setText("发布公告");
    }
    private void initListener() {
        btnReleaseNotice.setOnClickListener(this);
        titleGoBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_go_back:
                onBackPressed();
                break;
            case R.id.btn_release_notice:
                //// TODO: 2016/12/27 发布公告
                String content = etNoticeContent.getText().toString();
                if ("".equals(content)||content==null){
                    showToast(ctx,"内容不能为空!");
                    return;
                }
                addNoticeRequest("公告",content);
                break;
        }
    }

    /**
     * 发布公告网络请求
     * @param title 发布公告的标题(不显示,随意加个)
     * @param content 公告内容
     */
    private void addNoticeRequest(String title,String content){
        showDialog(ctx);
        params.clear();
        String strUrl = Urls.AddNotice;
        params.addStringRequest(ParamsKey.UserKey,myApplication.getUserKey());
        params.addStringRequest(ParamsKey.AddNotice_Title,title);
        params.addStringRequest(ParamsKey.AddNotice_Content,content);
        myOkHttpUtils.postRequest(strUrl, params, new IsSuccessCallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dismissDialog();
                showToast(ctx,"网络错误!");
            }

            @Override
            public void onResponse(IsSuccessBean response, int id) {
                dismissDialog();
                if (response.getState()==1){
                    showToast(ctx,response.getData().getMessage()+"");
                    if (response.getData().isIsSuccess()){
                        handler.sendEmptyMessage(ADD_NOTICE_REQUEST_OK);
                    }
                }else {
                    showToast(ctx,"提示:"+response.getMsg());
                }
            }
        });
    }
    public static final int ADD_NOTICE_REQUEST_OK = 8951;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case ADD_NOTICE_REQUEST_OK:
                    // TODO: 2017/1/16 添加公告成功
                    setResult(RESULT_OK);
                    break;
            }
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}
